package com.lmm.lcbusiness.lcbusiness.pages.choosecity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.location.BDLocation;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lmm.dresswisdom.lmmframe.util.LogUtils;
import com.lmm.dresswisdom.lmmframe.util.ToastUtils;
import com.lmm.lcbusiness.lcbusiness.AppBaseActivity;
import com.lmm.lcbusiness.lcbusiness.R;
import com.lmm.lcbusiness.lcbusiness.config.AppInfoAction;
import com.lmm.lcbusiness.lcbusiness.config.AppKey;
import com.lmm.lcbusiness.lcbusiness.config.AppKeyValueUtils;
import com.lmm.lcbusiness.lcbusiness.diyviews.PhoneRecycleView;
import com.lmm.lcbusiness.lcbusiness.diyviews.SideBar;
import com.lmm.lcbusiness.lcbusiness.utils.GPSReturn;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xmgong
 *         选择城市
 */
@SuppressLint("Registered")
@Route(path = "/app/choosecity")
public class ChooseCityActivity extends AppBaseActivity implements ChooseCityContract.IView, GPSReturn, SideBar.OnTouchingLetterChangedListener {
    private ChooseCityPresent mPresent;

    PhoneRecycleView mPhoneRecycleView;
    CityNewAdapter mCityNewAdapter;
    Map<String, Integer> positons = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        setContentView(R.layout.activity_choosecity);
        setActionBarTitle("选择城市");
        mPhoneRecycleView = findViewById(R.id.phonerecycle);
        mPhoneRecycleView.setOnTouchingLetterChangedListener(this);
        mPresent = new ChooseCityPresent(this);
        if (mPresent.getDBCity() == 0) {
            mPresent.updateCityAction();
        } else {
            mPresent.getAllCitys();
        }
    }

    @Override
    public void retrunGPSInfo(BDLocation bdLocation) {
        if (bdLocation != null) {
            LogUtils.write(bdLocation.getAddrStr());
        }
    }

    @Override
    public void updateCitysFinish() {
        hideDefaultDialog();
        mPresent.getAllCitys();
    }

    @Override
    public void getCityReturn(List<AreaInfoModel> res) {
        //遍历获取所有字符变化的
        for (int i = 0; i < res.size() - 1; i++) {
            if (i == 0) {
                positons.put(mPresent.getPinYin(res.get(i).getName()), i);
            } else if (!mPresent.getPinYin(res.get(i).getName()).equals(mPresent.getPinYin(res.get(i + 1).getName()))) {
                positons.put(mPresent.getPinYin(res.get(i + 1).getName()), i + 1);
            }
        }

        if (mCityNewAdapter == null) {
            initAdapter(res);
        } else {
            mCityNewAdapter.setNewData(res);
        }
    }

    @Override
    public void initAdapter(List<AreaInfoModel> mdata) {
        mCityNewAdapter = new CityNewAdapter(mdata);
        mPhoneRecycleView.setBaseQuickAdapter(mCityNewAdapter);
        mCityNewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AreaInfoModel curplace = mCityNewAdapter.getData().get(position);
                returnMainView(curplace);
            }
        });
        mPhoneRecycleView.addSearchBar();
        mPhoneRecycleView.getSearchView().setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    mPresent.getAllCitys();
                } else {
                    mPresent.searchCity(newText);
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    mPresent.getAllCitys();
                } else {
                    mPresent.searchCity(newText);
                }
                return true;
            }
        });
    }

    @Override
    public void onTouchingLetterChanged(String s) {
        ToastUtils.showToastShort(s);
        if (getPosition(s) > -1) {
            mPhoneRecycleView.moveToItem(getPosition(s));
        }
    }

    private int getPosition(String s) {
        int i = -1;
        if (positons.containsKey(s.toLowerCase())) {
            i = positons.get(s.toLowerCase());
        }
        return i;
    }


    private void returnMainView(AreaInfoModel curplace) {
        AppKeyValueUtils.setValue(AppKey.CurrentPlace, curplace.getName());
        AppKeyValueUtils.setValue(AppKey.CurrentPlaceCode, curplace.getCityCode());
        AppInfoAction.addHistoryKey(curplace.getName());

        Map<String, String> map = mPresent.getShenShiQu(curplace.getCityCode());

        Intent mintent = new Intent();
        mintent.putExtra("shen", map.get("shen"));
        mintent.putExtra("shi", map.get("shi"));
        mintent.putExtra("xian", map.get("xian"));
        mintent.putExtra("city", curplace.getName());
        mintent.putExtra("county", "");
        mintent.putExtra("code", curplace.getCityCode());
        setResult(RESULT_OK, mintent);
        finish();
    }

}